package
{

import com.util.Console;
import com.util.FpsMeter;
import com.util.TestButton;

import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.system.System;
import flash.text.TextField;

import leaf.science.ui.camera.Camera;
import leaf.science.ui.camera.DemoHotspot;
import leaf.science.ui.camera.DemoToolTip;
import leaf.science.ui.camera.DemosCamera;
import leaf.science.ui.jogwheel.JogWheel;


[SWF(frameRate="60", backgroundColor="#ECECEC", width="1024", height="680")]
public class Test extends MovieClip
{

	static public var DEBUG:TextField;

	public var debug:TextField;
	public var camera:Camera;
	public var jogWheel:JogWheel;

	private var _center:Sprite;

	private var _test1:TestButton;
	private var _test2:TestButton;
	private var _test3:TestButton;
	private var _test4:TestButton;
	
	private var _loader:Loader = new Loader();
	private var _preloader:CameraPreloader;
	
	private var _fps:FpsMeter;
	
	public function Test():void
	{
		stage.scaleMode = StageScaleMode.NO_SCALE;
		stage.align = StageAlign.TOP_LEFT;
		stage.quality = StageQuality.HIGH;
		stage.addEventListener( Event.RESIZE, onStageResize );

		debug = new TextField();
		debug.mouseEnabled = false;
		DEBUG = debug;

		jogWheel = new JogWheel();
		jogWheel.addEventListener( Event.CHANGE, onJogWheelChange );
		addChild( jogWheel );

		_loader = new Loader;
		_loader.mouseEnabled = false;
		_loader.contentLoaderInfo.addEventListener( Event.OPEN, onLoadOpen, false, 0, true );
		_loader.contentLoaderInfo.addEventListener( Event.INIT, onLoadInit, false, 0, true );
		_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoadComplete, false, 0, true );
		_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onLoadProgress, false, 0, true );
		_loader.load( new URLRequest( "../swf/DemosCamera.swf" ));
//		_loader.load( new URLRequest( "../swf/FeaturesCamera.swf" ));
		addChild( _loader );

		_center = new Sprite;
		_center.mouseEnabled = false;
		addChild( _center );

		_test1 = new TestButton( "front" );
		_test1.y = 50;
		_test1.width = 100;
		_test1.onClick = onTest1;
		addChild( _test1 );

		_test2 = new TestButton( "back" );
		_test2.y = 75;
		_test2.width = 100;
		_test2.onClick = onTest2;
		addChild( _test2 );

		_test3 = new TestButton( "left" );
		_test3.y = 100;
		_test3.width = 100;
		_test3.onClick = onTest3;
		addChild( _test3 );

		_test4 = new TestButton( "right" );
		_test4.y = 125;
		_test4.width = 100;
		_test4.onClick = onTest4;
		addChild( _test4 );
		
		addChild( debug );
		onStageResize();
		
		_fps = new FpsMeter;
		addChild( _fps );
		
//		var p:Preloader = new Preloader( 0xffffff );
//		p.x = stage.stageWidth/2;
//		p.y = stage.stageHeight/2;
//		addChild( p );
		
		stage.addEventListener( Event.ENTER_FRAME, onEnterFrame );
	}

	private function onLoadOpen( evt:Event ):void
	{
		_preloader = new CameraPreloader();
		_center.addChild( _preloader );
	}

	private function onLoadInit( evt:Event ):void
	{
		//camera = MovieClip( _loader.content ).getChildByName( "camera" ) as DemosCamera;
		camera = MovieClip( _loader.content ) as Camera;
		camera.addEventListener( Event.CHANGE, onCameraChange );
		camera.addEventListener( MouseEvent.CLICK, onCameraClick );
		//camera.scaleX = camera.scaleY = 0.85;
		camera.x = -Math.round( camera.width/2 );
		camera.y = -Math.round( camera.height/2 );
	}
	
	private function onLoadProgress( evt:ProgressEvent ):void
	{
		jogWheel.range.end = evt.bytesLoaded/evt.bytesTotal;
	}

	private function onLoadComplete( evt:Event ):void
	{
		_center.removeChild( _preloader );
		_preloader = null;
	}
	
	private function unload():void
	{
		camera = null;
		removeChild( _loader );
		_loader.unload();
		_loader = null;
//		System.gc();
//		System.gc();
		Console.log( System.totalMemory/1024/1024 );
	}

	private function onJogWheelChange( evt:Event ):void
	{
		DemosCamera( camera ).closeAllToolTipsNow();
		camera.frame = Math.round( camera.totalFrames * jogWheel.value );
	}
	
	private function onCameraChange( evt:Event ):void
	{
		jogWheel.value = camera.angle/360;
	}

	private function onCameraClick( evt:MouseEvent ):void
	{
		if( evt.target is DemoHotspot )
		{
			click( evt.target as DemoHotspot );
		}
		if( evt.target is DemoToolTip )
		{
			click( DemoToolTip( evt.target ).hotspot );
		}
	}

	private function click( h:DemoHotspot ):void
	{
		trace( "CLICKED " + h );
		//DemosCamera( camera ).hotspot = h;
		DemosCamera( camera ).closeAllToolTipsNow();
	}

	private function onStageResize( evt:Event=null ):void
	{
		jogWheel.x = Math.round( stage.stageWidth/2 );
		jogWheel.y = Math.round( stage.stageHeight/2 );

		_loader.x = Math.round( stage.stageWidth/2 );
		_loader.y = Math.round( stage.stageHeight/2 );

		_center.x = Math.round( stage.stageWidth/2 );
		_center.y = Math.round( stage.stageHeight/2 );
	}
	
	private function onEnterFrame( evt:Event ):void
	{
		_fps.update();
	}
	
	private function onTest1():void
	{
		//camera.gotoLabel( "front" );
		//unload();
		var demoCamera:DemosCamera = camera as DemosCamera;
		demoCamera.hotspot = demoCamera.hotspots[0];
	}

	private function onTest2():void
	{
		//camera.gotoLabel( "back" );
		var demoCamera:DemosCamera = camera as DemosCamera;
		demoCamera.hotspot = demoCamera.hotspots[1];
	}

	private function onTest3():void
	{
//		camera.gotoLabel( "left" );
		DemosCamera( camera ).showHotspots();

	}

	private function onTest4():void
	{
//		camera.gotoLabel( "right" );
		DemosCamera( camera ).hideHotspots();
	}

}
}